package com.wj.facedetect.fragment;


import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wj.facedetect.R;
import com.wj.facedetect.activity.commom.PhotoPickerActivity;
import com.wj.facedetect.activity.home.FaceCardActiviy;
import com.wj.facedetect.activity.home.NewNToNActivity;
import com.wj.facedetect.activity.home.OneToNPreActiviy;
import com.wj.facedetect.activity.signin.SignInActivity;
import com.wj.facedetect.util.CameraUtil;
import com.wj.facedetect.util.CommonUtils;
import com.wj.facedetect.util.DialogUtil;
import com.wj.facedetect.util.FileUtil;
import com.wj.facedetect.util.PreferencesUtils;

import java.io.File;
import java.util.ArrayList;

import static android.app.Activity.RESULT_FIRST_USER;
import static android.app.Activity.RESULT_OK;
import static com.wj.facedetect.bean.common.CameraCode.IMESSAGE_IMAGE;
import static com.wj.facedetect.bean.common.CameraCode.REQUEST_CODE_LOAD_IMAGE;
import static com.wj.facedetect.bean.common.CameraCode.REQUEST_CODE_TAKE_PICTURE;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment implements View.OnClickListener {

    private View mainView;
    private RelativeLayout rlNtoN, rlOneTon, rlRzhy, rlCheckResult, rlRyqd;
    private TextView tvRight;
    private Uri mPhotoUri;
    //
    public final int CODE_TAKE_PHOTO = 1;//相机RequestCode
    public final int CODE_PRE_PHOTO = CODE_TAKE_PHOTO + 1;


    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mainView = inflater.inflate(R.layout.fragment_home, container, false);
        initView();
        return mainView;
    }

    private void initView() {
        rlNtoN = mainView.findViewById(R.id.rl_n_to_n);
        rlOneTon = mainView.findViewById(R.id.rl_one_to_n);
        rlRzhy = mainView.findViewById(R.id.rl_rzhy);
        rlCheckResult = mainView.findViewById(R.id.rl_check_result);
        rlRyqd = mainView.findViewById(R.id.rl_ryqd);
        rlRyqd.setOnClickListener(this);
        rlNtoN.setOnClickListener(this);
        rlOneTon.setOnClickListener(this);
        rlRzhy.setOnClickListener(this);
        rlCheckResult.setOnClickListener(this);
        tvRight = mainView.findViewById(R.id.tv_right);
        tvRight.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_n_to_n:
                int kuid = PreferencesUtils.getInstance().getSharePreInt(PreferencesUtils.KU_ID);
                if (kuid == Integer.MIN_VALUE) {
                    DialogUtil.showErrorMsg(getActivity(), "请到设置模块，设置检查范围");
                    return;
                }
                getActivity().startActivity(new Intent(getActivity(), NewNToNActivity.class));
                break;
            case R.id.rl_one_to_n: {
                int kuidx = PreferencesUtils.getInstance().getSharePreInt(PreferencesUtils.KU_ID);
                if (kuidx == Integer.MIN_VALUE) {
                    DialogUtil.showErrorMsg(getActivity(), "请到设置模块，设置检查范围");
                    return;
                }
                DialogUtil.showChoosePhotoOrTakePhotoDialog(getActivity(), new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Dialog dialog = (Dialog) view.getTag();
                        if (dialog != null) {
                            dialog.dismiss();
                        }
                        Intent takeIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        Uri photoUri = CameraUtil.buildPhotoUri(getContext());
                        CameraUtil.grantPermission(getContext(), takeIntent, photoUri);
                        mPhotoUri = photoUri;
                        mFilePath = FileUtil.getFile(photoUri).getAbsolutePath();
                        takeIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
                        startActivityForResult(takeIntent, CODE_TAKE_PHOTO);
                    }
                }, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Dialog dialog = (Dialog) view.getTag();
                        if (dialog != null) {
                            dialog.dismiss();
                        }
                        handleSelectImageIntent();
                    }
                });


            }
            break;
            case R.id.rl_rzhy: {
                int kuidm = PreferencesUtils.getInstance().getSharePreInt(PreferencesUtils.KU_ID);
                if (kuidm == Integer.MIN_VALUE) {
                    DialogUtil.showErrorMsg(getActivity(), "请到设置模块，设置检查范围");
                    return;
                }
                Intent intent = new Intent(getActivity(), FaceCardActiviy.class);
                startActivity(intent);
            }
            break;
            case R.id.rl_check_result:
//                Intent intent = new Intent(getActivity(), FaceCheckResultActivity.class);
//                getActivity().startActivity(intent);
                break;
            case R.id.tv_right:
                break;
            case R.id.rl_ryqd:
                //人员签到
                Intent intentRqyd = new Intent(getActivity(), SignInActivity.class);
                //Intent intentRqyd = new Intent(getActivity(), SignInDemoActivity.class);
                getActivity().startActivity(intentRqyd);
                break;
            default:
                break;
        }
    }

    /**
     * 选择图片
     */
    private void handleSelectImageIntent() {
        Intent intent = new Intent(getActivity(), PhotoPickerActivity.class);
        intent.putExtra(PhotoPickerActivity.EXTRA_SHOW_CAMERA, true);
        intent.putExtra(PhotoPickerActivity.EXTRA_SELECT_MODE, PhotoPickerActivity.MODE_SINGLE);
        intent.putExtra(PhotoPickerActivity.EXTRA_MAX_MUN, 9);
        startActivityForResult(intent, REQUEST_CODE_LOAD_IMAGE);
    }

    private String mFilePath;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case CODE_TAKE_PHOTO:
                if (resultCode == RESULT_OK) {
                    OneToNPreActiviy.startActivity(null, this, mFilePath, CODE_PRE_PHOTO);
                }
                break;
            case CODE_PRE_PHOTO:
                if (resultCode == RESULT_FIRST_USER) {
                    rlOneTon.callOnClick();
                }
                break;
        }
        if (data == null) {
            return;
        }
        //照相或选择图片
        if (requestCode == REQUEST_CODE_TAKE_PICTURE || requestCode == REQUEST_CODE_LOAD_IMAGE) {
            if (requestCode == REQUEST_CODE_LOAD_IMAGE) {
                ArrayList<String> result = data.getStringArrayListExtra(PhotoPickerActivity.KEY_RESULT);
                if (result != null && !result.isEmpty()) {
                    mFilePath = result.get(0);
                    mPhotoUri = Uri.parse(result.get(0));
                } else {
                    mFilePath = CommonUtils.resolvePhotoFromIntent(getActivity(), data, IMESSAGE_IMAGE);
                    mPhotoUri = Uri.parse(CommonUtils.resolvePhotoFromIntent(getActivity(), data, IMESSAGE_IMAGE));
                }
                OneToNPreActiviy.startActivity(null, this, mFilePath, CODE_PRE_PHOTO);
            }
            if (TextUtils.isEmpty(mFilePath)) {
                return;
            }
            File file = new File(mFilePath);
            if (file == null || !file.exists()) {
                return;
            }
            return;
        }
    }

}
